/*
 * // +-------------------------------------------------------------------------------------------------
 * // |                 有你就好 [ 有节骨乃坚，无心品自端 ]     <http://encoding.wang>
 * // +-------------------------------------------------------------------------------------------------
 * // |                             独在异乡为异客         每逢佳节倍思亲
 * // +-------------------------------------------------------------------------------------------------
 * // |                 联系:   <707069100@qq.com>      <http://weibo.com/513778937>
 * // +-------------------------------------------------------------------------------------------------
 */

// -----------------------------------------------------------------------------------------------------
// +----------------------------------------------------------------------------------------------------
// |                   ErYang出品 属于小极品          共同学习    共同进步
// +----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------


package wang.encoding.mroot.plugin.ueditor.define;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import wang.encoding.mroot.plugin.ueditor.Encoder;


/**
 * 多状态集合状态
 * <p>
 * 包含了多个状态的集合 其本身自己也是一个状态
 *
 * @author ErYang
 */
public class MultiState implements State {

    private boolean state;
    private String info = null;
    private Map<String, Long> intMap = new HashMap<>();
    private Map<String, String> infoMap = new HashMap<>();
    private List<String> stateList = new ArrayList<>();

    public MultiState(boolean state) {
        this.state = state;
    }

    public MultiState(boolean state, String info) {
        this.state = state;
        this.info = info;
    }

    public MultiState(boolean state, int infoKey) {
        this.state = state;
        this.info = AppInfo.getStateInfo(infoKey);
    }

    @Override
    public Boolean isSuccess() {
        return this.state;
    }

    public void addState(State state) {
        stateList.add(state.toJSONString());
    }

    /**
     * 该方法调用无效果
     */
    @Override
    public void putInfo(String name, String val) {
        this.infoMap.put(name, val);
    }

    // -------------------------------------------------------------------------------------------------

    @Override
    public String toJSONString() {
        String stateVal = this.isSuccess() ? AppInfo.getStateInfo(AppInfo.SUCCESS) : this.info;
        StringBuilder builder = new StringBuilder();
        builder.append("{\"state\": \"").append(stateVal).append("\"");
        // 数字转换
        Iterator<String> iterator = this.intMap.keySet().iterator();
        while (iterator.hasNext()) {
            stateVal = iterator.next();
            builder.append(",\"").append(stateVal).append("\": ").append(this.intMap.get(stateVal));
        }
        iterator = this.infoMap.keySet().iterator();
        while (iterator.hasNext()) {
            stateVal = iterator.next();
            builder.append(",\"").append(stateVal).append("\": \"").append(this.infoMap.get(stateVal)).append("\"");
        }
        builder.append(", list: [");
        iterator = this.stateList.iterator();
        while (iterator.hasNext()) {
            builder.append(iterator.next()).append(",");
        }

        if (this.stateList.size() > 0) {
            builder.deleteCharAt(builder.length() - 1);
        }
        builder.append(" ]}");
        return Encoder.toUnicode(builder.toString());
    }

    // -------------------------------------------------------------------------------------------------

    @Override
    public void putInfo(String name, Long val) {
        this.intMap.put(name, val);
    }

    // -------------------------------------------------------------------------------------------------

}

// -----------------------------------------------------------------------------------------------------

// End MultiState class

/* End of file MultiState.kt */
/* Location: ./src/main/kotlin/wang/encoding/mroot/plugin/ueditor/define/MultiState.kt */

// -----------------------------------------------------------------------------------------------------
// +----------------------------------------------------------------------------------------------------
// |                           ErYang出品 属于小极品  O(∩_∩)O~~   共同学习    共同进步
// +----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------
